package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Result;
import com.example.entity.SAttribute;
import com.example.service.SAttributeService;
import com.example.vo.AttributeVO;
import io.swagger.annotations.ApiOperation;
import org.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/attribute")
public class SAttributeController {
    @Autowired
    private SAttributeService attributeService;

    @ApiOperation("更新新增具体属性")
    @PostMapping("/addSpecificAttribute")
    public Result<?> addSpuAttribute(@RequestParam String attributeName,@RequestParam String value){
        //先根据属性名找到对象
        LambdaQueryWrapper<SAttribute> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SAttribute::getName, attributeName);
        SAttribute one = attributeService.getOne(wrapper);
        String options = one.getOptions();
        // 解析原始字符串为 JSONArray
        JSONArray originalArr = new JSONArray(options);
        // 添加新元素
        originalArr.put(value);

        // 将 JSONArray 转换为字符串
        String resultStr = originalArr.toString();
        one.setOptions(resultStr);
        attributeService.update(one,wrapper);
        return Result.success();
    }

    @ApiOperation("删除Attribute具体属性")
    @DeleteMapping("/deleteAttribute")
    public Result<?> addSkuAttribute(@RequestParam String attributeName,@RequestParam String value){
        //先根据属性名找到对象
        LambdaQueryWrapper<SAttribute> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SAttribute::getName, attributeName);
        SAttribute one = attributeService.getOne(wrapper);
        String options = one.getOptions();
        // 解析原始字符串为 JSONArray
        JSONArray originalArr = new JSONArray(options);
        for (int i=0;i<originalArr.length();i++){
            if (originalArr.get(i).equals(value))
                originalArr.remove(i);
        }
        // 将 JSONArray 转换为字符串
        String resultStr = originalArr.toString();
        one.setOptions(resultStr);
        attributeService.update(one,wrapper);
        return Result.success();
    }

    @ApiOperation("查询所有spu属性返回属性集合")
    @GetMapping("/spuAttribute")
    public Result<List<String>> getSpuAttribute(){
        LambdaQueryWrapper<SAttribute> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SAttribute::getType,0).select(SAttribute::getName);
        List<String> list = attributeService.listObjs(wrapper);
        return Result.success(list);
    }

    @ApiOperation("查询所有sku属性返回属性集合")
    @GetMapping("/skuAttribute")
    public Result<List<String>> getSkuAttribute(){
        LambdaQueryWrapper<SAttribute> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SAttribute::getType,1).select(SAttribute::getName);
        List<String> list = attributeService.listObjs(wrapper);
        return Result.success(list);
    }


    @ApiOperation("查询基本类型")
    @GetMapping("/getBasic")
    public Result<AttributeVO> getBasicSpuAttr() {
        //可动态修改要获取的属性, 如下面的getSizeAndColor
        List<String> list = Arrays.asList("material", "function", "assurance", "people");
        AttributeVO attributeVO = attributeService.getAttribute(list);
        return Result.success(attributeVO);
    }

    @ApiOperation("查询颜色,尺码")
    @GetMapping("/getSizeColor")
    public Result<AttributeVO> getSizeAndColor() {
        List<String> list = Arrays.asList("size","color");
        AttributeVO attributeVO = attributeService.getAttribute(list);
        return Result.success(attributeVO);
    }






    @PostMapping("/add")
    public Result<?> addBrand(@RequestBody SAttribute sAttribute){

        attributeService.save(sAttribute);
        return Result.success();
    }

    @DeleteMapping("/delete")
    public Result<?> deleteBrands(@RequestBody List<Long> list){
        attributeService.removeBatchByIds(list);
        return Result.success();
    }


    @PostMapping("/update")
    public Result<?> updateBrand(@RequestBody SAttribute sAttribute){
        LambdaQueryWrapper<SAttribute> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SAttribute::getId,sAttribute.getId());
        attributeService.update(sAttribute,wrapper);
        return Result.success();
    }



    @GetMapping("/page")
    public Result<?> findPage(@RequestParam(required = false, defaultValue = "") String name,
                              @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                              @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        LambdaQueryWrapper<SAttribute> query = Wrappers.<SAttribute>lambdaQuery().like(SAttribute::getName, name);
        IPage<SAttribute> page = attributeService.page(new Page<>(pageNum, pageSize), query);
        return Result.success(page);
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        attributeService.removeById(id);
        return Result.success();
    }


}
